import pandas as pd
from com.haisen.financial import settle_period
"""
selttle_util测试类

"""


def get_biz_settle_period(_dic, _area_biz):
    _tmp_dic = _dic
    for _key in _area_biz:
        if isinstance(_tmp_dic, dict):
            if _key in _tmp_dic:
                _tmp_dic = _tmp_dic[_key]
            else:
                return ''
    return _tmp_dic

# _biz_period业务结算周期, _value: 计算总金额，带优化一并计算


def get_biz_value_period(_period, _settle_sum, _give=0):
    _biz_settle = {}
    for _period, _rule in _period.items():
        # 档次是否分有赠送，无赠送
        print("====字典 周期 = %(n1)s, 结算金额 = %(n2)s" % {'n1': _period, 'n2': _rule})
        if 'give' in _rule:
            if _give == 1:
                _biz_settle[_period] = _settle_sum * _rule['give']['shared'] * _rule['give']['losed']
            else:
                _biz_settle[_period] = _settle_sum * _rule['nogive']['shared'] * _rule['nogive']['losed']
        else:
            _biz_settle[_period] = _settle_sum * _rule['shared'] * _rule['losed']
    return _biz_settle


# 测试思路一、将一行复制为多行
df = df = pd.DataFrame({'phone': ['13929503081', '13929503041', '13929509012'], 'area': ['惠州', '惠州', '惠州'],
                       'biz': ['入网', '套餐升档', '老客户融宽拓展'], 'value': [29, 40, 120]})
# df.set_index(['phone', 'area'])

# 传入一个多重索引，号码+业务名称，根据结算周期对业务进行拆分
settle_period_dic = settle_period.settle_period_dic
# _area = '惠州'
# _biz = '入网'
# if _area in settle_period_dic:
#     print(type(settle_period_dic[_area]), settle_period_dic[_area])
#     # _biz_dic = eval(settle_period_dic[_area])
#     _biz_dic = settle_period_dic[_area]
#     if _biz in _biz_dic:
#         print(settle_period_dic[_area][_biz])

# 采用递归函数判断字典的key和值
ret = get_biz_settle_period(settle_period_dic, ['茂名', '套餐升档'])
print("=====函数遍历返回结果===\n", ret)
# 根据结果集拆分多行
# 变换成T+1:10,T+2:20 字样
# _value = 100
# _zengsong = 0
# for k, v in ret.items():
#     # 档次是否分有赠送，无赠送
#     print("====字典 key = %(n1)s, value = %(n2)s" % {'n1': k, 'n2': v})
#     if 'give' in v:
#         if _zengsong == 1:
#             cal = _value*v['give']['shared']*v['give']['losed']
#         else:
#             cal = _value*v['nogive']['shared'] * v['nogive']['losed']
#     else:
#         cal = _value*v['shared'] * v['losed']
#     print(cal)
#
_settle_money = get_biz_value_period(ret, 100, 1)
print("====结算金额周期====\n", _settle_money)
print(df)
records = df.to_dict(orient='records')
print(records)
bizs = []
for li in records:
    _rule = get_biz_settle_period(settle_period_dic, [li['area'], li['biz']])
    print(_rule)
    _settle_money = get_biz_value_period(_rule, li['value'])
    for _period, _money in _settle_money.items():
        # _li_tmp = licopy()
        # _li_tmp[_period] = _money
        # bizs.append(_li_tmp)
        #   methon2
        li[_period] = _money
    bizs.append(li)
print(bizs)
new_df = pd.DataFrame(bizs)
print("====return new datafram ===\n", new_df.info(), new_df)

df
exit()







# 测试思路二，拼湊字串串，让后扩展为列

df = pd.DataFrame({'phone': ['13929503081', '13929503041', '13929509012'], 'area': ['惠州', '惠州', '惠州'],
                   'biz': ['入网', '套餐升档', '老客户融宽拓展'], 'value': [29, 40, 120],
                   'period': ['"T2":14.5, "T3":14.5', '"T1": 10,"T2":10,"T3":10, "T4":10',
                              '"T2:"20,"T3":20,"T4":20,"T5":20, "T6":20, "T7":20']})

print(df.info(), df)

info_df = df['period'].str.split(',', expand=True)
print("=======拆分后的列=====\n", info_df)
print("=====思路测试结果，拆分后的列名对应问题需要解决=====，排列的位置为对立，列名未指定")
# settle_period_dic = settle_period.settle_period_dic


